#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2024/1/27 15:35
# @Author : 闫峰
# @File    : sycdata.py
from common.setting import ensure_path_sep
from utils.logging_tool.init_logger import  init_logger
from utils.mysqlUtils.db_pool import Database
from utils.times_tool.time_control import count_time

INFO, ERROR, WARNING = init_logger()


class SycData:
    zhcxkj_center_listing_comprehensive = Database(ensure_path_sep("\\config\\shopeev2_config.yaml.bak"), "zhcxkj_center_listing_comprehensive")
    java_test = Database(ensure_path_sep("\\config\\shopeev2_config.yaml.bak"), "test_jdw_database")

    @count_time
    def syc_data(self,souece_id):
        index = 0
        count = 0
        while True:
            syc_sql ="""
            SELECT
                * 
            FROM
                zhcxkj_center_listing_comprehensive.t_jdwalmart_listing 
            WHERE
                id >% s
                AND source_id =% s
                ORDER BY id 
                LIMIT 1000;
            """
            syc_data_list = self.zhcxkj_center_listing_comprehensive.fetch_all_tuple(syc_sql,(index,souece_id))
            if not len(syc_data_list):
                break

            index = syc_data_list[-1][0]
            jdwalmart_listing_id_set = set(map(lambda x: x[0], syc_data_list))
            get_data_list = """
                SELECT * 
            FROM zhcxkj_center_listing_comprehensive.t_jdwalmart_listing_detail
            WHERE jdwalmart_listing_id in %s
                """
            data_list = self.zhcxkj_center_listing_comprehensive.fetch_all_tuple(get_data_list,
                                                                                 (jdwalmart_listing_id_set,))
            if len(data_list):
                insert_sql = """
                INSERT IGNORE INTO test_zhcxkj_center_listing_comprehensive.t_jdwalmart_listing_detail (
                id,
                jdwalmart_listing_id,
                customer_id,
                source_id,
                category_id,
                category_name,
                brand_id,
                brand,
                properties,
                images,
                platform_ext,
                create_by,
                create_time,
                update_by,
                update_time,
                is_deleted
                ) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
                """
                rows = self.java_test.execute_many(insert_sql, data_list)
                if rows == 0:
                    print(f"插入=0，jdwalmart_listing_id_set={jdwalmart_listing_id_set}")
                count += rows
                print(f"目前插入{count}")
#             insert_sql = """
# INSERT IGNORE INTO test_zhcxkj_center_listing_comprehensive.t_jdwalmart_listing (
#     id,
#     customer_id,
#     execute_queue_detail_id,
#     source_id,
#     source_name,
#     title,
#     spu,
#     sku,
#     parent_source_sku,
#     source_sku,
#     group_sku,
#     product_id,
#     type,
#     status,
#     product_status,
#     tort,
#     upc,
#     sale_price,
#     discount_price,
#     quality,
#     properties,
#     images,
#     item_id,
#     currency,
#     country,
#     country_icon,
#     comb,
#     default_sku,
#     listing_method,
#     listing_person,
#     salesman_id,
#     salesman,
#     publish_time,
#     un_publish_time,
#     remark,
#     create_by,
#     create_time,
#     update_by,
#     update_time,
#     is_deleted
# ) VALUES (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)
#             """
#             rows = self.java_test.execute_many(insert_sql,syc_data_list)



if __name__ == '__main__':
    SycData().syc_data(9948)